package com.cn.wanxi.dao.impl;

import com.cn.wanxi.dao.UserDao;
import com.cn.wanxi.model.UserModel;
import com.cn.wanxi.util.GetSqlSession;
import com.cn.wanxi.util.JDBC;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

public class UserImpl implements UserDao {
    private String add = "com.cn.wanxi.dao.UserDao.";

    @Override
    public UserModel findByUserName(String username) {
        return GetSqlSession.getSqlSession().selectOne(add + "findByUserName", username);
    }

    @Override
    public int add(UserModel userModel) {
        return GetSqlSession.getSqlSession().insert("com.cn.wanxi.dao.UserDao.", userModel);
    }

    /**
     * 根据用户名和密码判断当前查询的数据是否有值
     * 1.如果有则进入while循环，直接返回一个对象，只不过里面为空值  null
     *
     * @param userModel
     * @return
     */
    @Override
    public UserModel findByUsernameAndPassword(UserModel userModel) {

        return GetSqlSession.getSqlSession().selectOne(add + "findByUsernameAndPassword", userModel);
    }

    @Override
    public List<UserModel> findAll(UserModel userModel) {
        return GetSqlSession.getSqlSession().selectList(add + "findAll", userModel);
    }

    @Override
    public int del(Integer id) {
//        String sql = "delete from user where id=" + id;
//        return JDBC.update(sql);
        return GetSqlSession.getSqlSession().delete(add + "del", id);
    }

    @Override
    public UserModel findById(Integer id) {
        return GetSqlSession.getSqlSession().selectOne(add + "findById", id);
    }

    @Override
    public int update(UserModel userModel) {
        String sql = "UPDATE `user` SET \n" +
                "`phone` = '" + userModel.getPhone() + "', \n" +
                "`sex` = '" + userModel.getSex() + "', \n" +
                "`birthday` = '" + userModel.getBirthday() + "',\n" +
                " `hobby` = '" + userModel.getHobby() + "', \n" +
                " `email` = '" + userModel.getEmail() + "' \n" +
                " WHERE (`id` = '" + userModel.getId() + "')";
        return JDBC.update(sql);
    }

    @Override
    public Integer getCount(UserModel userModel) {
        return GetSqlSession.getSqlSession().selectOne(add + "getCount", userModel);
    }
}
